{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## **实验五  栈的实现**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实验要求:1-建立顺序栈,实现栈的基本操作.2-完成用以实现10进制数转换成任意进制(2~9)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1-1,建立顺序栈"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Stack(object):\n",
    "    def __init__(self):\n",
    "        self.stack = []\n",
    "\n",
    "    def push(self,item):    # 元素进栈\n",
    "        self.stack.append(item)\n",
    "\n",
    "    def pop(self):  # 从栈顶删除item\n",
    "        if self.stack:\n",
    "            return self.stack.pop()\n",
    "        else:\n",
    "            raise LookupError('栈为空')\n",
    "\n",
    "\n",
    "    def isEmpty(self): # 如果栈为空\n",
    "        return self.stack == []\n",
    "\n",
    "    def peek(self):    #查看栈顶元素\n",
    "        return self.stack[-1]\n",
    "    \n",
    "    def size(self):    # 查看栈大小\n",
    "        return len(self.stack)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1-2,实现栈的基本操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 初始化栈\n",
    "s = Stack()\n",
    "# 判断栈空\n",
    "s.isEmpty()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 元素入栈\n",
    "s.push(4)\n",
    "s.push('dog')\n",
    "s.push(True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看栈顶元素\n",
    "s.peek()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除栈顶元素并再次查看栈顶元素\n",
    "s.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 入栈并查看栈大小\n",
    "s.push('cat')\n",
    "s.push(2)\n",
    "s.size()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2,进制转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def BaseConversion(num,base):\n",
    "    stack = Stack()\n",
    "    digits = \"0123456789ABCDEF\"\n",
    "    while num >0:\n",
    "        remainder = num % base\n",
    "        stack.push(remainder)\n",
    "        num //= base\n",
    "    BaseString = \"\"\n",
    "    while not stack.isEmpty():\n",
    "        BaseString = BaseString + digits[stack.pop()]\n",
    "    return  BaseString\n",
    "        \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入要转换的数值: 1666\n",
      "请问要转换成几进制? 16\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1666转换成16进制后的数值为: 682\n"
     ]
    }
   ],
   "source": [
    "num = int(input('请输入要转换的数值:'))\n",
    "base = int(input('请问要转换成几进制?'))\n",
    "print('{}转换成{}进制后的数值为:'.format(num,base),BaseConversion(num,base))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "xpython",
   "language": "python",
   "name": "xpython"
  },
  "language_info": {
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
